import { Component, OnInit, Injector, ViewChild } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/table';
import { Paginator, LazyLoadEvent } from 'primeng/primeng';
import {
    ListDtoExtensionsItem,
    Stock_AccountBaseListDto,
    StockZT_AccountBaseServiceProxy,
    StockType,
    StockZT_RoomServiceProxy,
    StockZT_IntoBillItemServiceProxy
} from '@shared/service-proxies/stock-service-proxies';
import { UserDataPermissionServiceProxy } from '@shared/service-proxies/stock-service-proxies';
import { CreateOrEditMarketOutBillModalComponent } from '../out/create-or-edit-market-out-bill-item-modal.component';
import { AppConsts } from '@shared/AppConsts';

@Component({
    templateUrl: './mark-account.component.html',
    animations: [appModuleAnimation()]
})
export class MarkAccountComponent extends AppComponentBase implements OnInit {
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('createOrEditMarketOutBillModal')
    createOrEditMarketOutBillModal: CreateOrEditMarketOutBillModalComponent;
    // filters
    f_MaterialCode = '';
    f_MaterialName = '';
    f_TradeMark = '';
    f_Station = '';
    f_TecCondition = '';
    f_Specification = '';
    f_QualityLevel = '';
    f_InventBatchID = '';
    f_WarehouseName = '';
    selectedWareHouse;
    userDataPermissions;
    roomOption;

    extensions: Array<ListDtoExtensionsItem>;
    selectedAccounts = new Array<Stock_AccountBaseListDto>();
    useableOptions = [{ label: '全部', value: undefined }, { label: '可用', value: true }];
    isuseable;
    purchaseOption;
    selectedPurchaseType;
    constructor(
        injector: Injector,
        private _accountService: StockZT_AccountBaseServiceProxy,
        private _userDataPermissionServiceProxy: UserDataPermissionServiceProxy,
        private _RoomServiceProxy: StockZT_RoomServiceProxy,
        private _billService: StockZT_IntoBillItemServiceProxy
    ) {
        super(injector);
    }

    ngOnInit(): void {
        this._userDataPermissionServiceProxy.getUserDataPermissions('Warehouses').subscribe(result => {
            this._RoomServiceProxy.getRoomListByDepts(result.items, '市采物资库', '').subscribe(result2 => {
                this.roomOption = result2.items.map(e => {
                    return {
                        label: e.displayText,
                        value: e
                    };
                });
                this._billService.getPurchaseTypeByDataItem().subscribe(result1 => {
                    this.purchaseOption = result1.map(e => {
                        return {
                            label: e.displayText,
                            value: e
                        };
                    });
                    this.selectedPurchaseType = result1;
                    this._RoomServiceProxy.getRoomListByUserRole('市采物资库').subscribe(result2 => {
                        this.selectedWareHouse = result2.items;
                        this.getAccounts();
                    });
                });
            });
        });
    }

    printBillList() {

            window.open(
                AppConsts.printAddr + '/webroot/decision/view/report?viewlet=erp/SCWZTZ.cpt'
            );

    }
    clearQuery() {
        this.f_MaterialCode = '';
        this.f_MaterialName = '';
        this.f_TradeMark = '';
        this.f_Station = '';
        this.f_TecCondition = '';
        this.f_Specification = '';
        this.f_QualityLevel = '';
        this.f_InventBatchID = '';
        this.f_WarehouseName = '';
        this.selectedWareHouse;
        this.userDataPermissions;
    }

    addoutbase() {
        let keepGo = true;
        let t = this.selectedAccounts[0].f_PurchaseType;
        // this.selectedAccounts.forEach(element => {
        //     if (element.f_PurchaseType != t && keepGo == true) {
        //         keepGo = false;
        //         this.notify.error('请选择相同市采类型的台帐！');
        //     }
        //     if (element.f_UsableSum! <= 0 || (element.f_WarehouseName != '天津火箭市采物资库' && keepGo == true)) {
        //         keepGo = false;
        //         if (element.f_UsableSum! <= 0) {
        //             this.message.warn('可用数量必须>0！');
        //         } else {
        //             this.message.warn('仓库必须为天津火箭市采物资库！');
        //         }
        //     }
        // });

        if(this.selectedAccounts.some(p=>p.f_PurchaseType!=t))
        {
            this.notify.error('请选择相同市采类型的台帐！');
            return;
        }
        if (this.selectedAccounts.some(element=>element.f_UsableSum! <= 0 ))
        {
            this.message.warn('可用数量必须>0！');
            return;
        }
        if (this.selectedAccounts.some(element => element.f_WarehouseName != '天津火箭市采物资库'))
        {
            this.message.warn('仓库必须为天津火箭市采物资库！');
            return;
        }

         this.createOrEditMarketOutBillModal.addAccoutInfo(this.selectedAccounts);

    }
    getAccounts(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event) || event.first == undefined) {
            this.paginator.changePage(0);

            return;
        }
        if (!this.selectedWareHouse || !this.selectedPurchaseType) {
            return;
        }

        this.primengTableHelper.showLoadingIndicator();
        this._accountService
            .getAccountListExtension(
                undefined,
                this.f_MaterialCode,
                this.f_MaterialName,
                this.f_TradeMark,
                this.f_Station,
                this.f_TecCondition,
                this.f_Specification,
                this.f_InventBatchID,
                this.selectedWareHouse,

                undefined,
                undefined,
                undefined,
                undefined,
                undefined,
                undefined,
                this.isuseable,
                StockType.市采,
                this.selectedPurchaseType,
                '台帐',
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                this.extensions = result.items;
                this.primengTableHelper.totalRecordsCount = result.stock_AccountBaseListDto.totalCount;
                this.primengTableHelper.records = result.stock_AccountBaseListDto.items;
                this.primengTableHelper.hideLoadingIndicator();
                this.selectedAccounts = null;
            });
    }
}
